﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; 
using MySql.Data.MySqlClient;

namespace MySQLLib
{
    public class Character_jobs
    {
        string[,] _characater_jobs;
        private MySqlConnection conn;

        public void setConnection(MySqlConnection connection)
        {
            this.conn = connection;
        }

        public void refresh()
        {
            string[] column = new string[1];
            column[0] = "*";

            this._characater_jobs = Query.SelectQuery(this.conn, "character_jobs", column);
        }

        public string[,] Char_Jobs
        {
            get { return this._characater_jobs; }
        }

        public void addJob(string char_name, string job_alias, int jobtype, int exp = 0, int rank = 0, int jobstate = 0, int joblevel = 0)
        {
            string[] columns = { "character_name", "job_alias", "job_type", "job_experience", "job_rank", "job_state", "job_level" };
            string[] values = { char_name, job_alias, jobtype.ToString(), exp.ToString(), rank.ToString(), jobstate.ToString(), joblevel.ToString() };
            Query.InsertQuery(this.conn, "character_jobs", columns, values);
        }

        public int getJobExp(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            int exp;
            Int32.TryParse(this._characater_jobs[id - 1, 4], out exp);

            return exp;
        }

        public int getJobRank(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            int rank;
            Int32.TryParse(this._characater_jobs[id - 1, 5], out rank);

            return rank;
        }

        public int getJobLevel(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            int level;
            Int32.TryParse(this._characater_jobs[id - 1, 7], out level);

            return level;
        }

        public int getJobState(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            int state;
            Int32.TryParse(this._characater_jobs[id - 1, 6], out state);

            return state;
        }

        public string getAlias(int id, bool refresh = false)
        {
            if (refresh)
                this.refresh();

            return this._characater_jobs[id - 1, 2];
        }

        public void setExperience(int id, int exp)
        {
            Query.UpdateQuery(this.conn, "character_jobs", "job_experience", exp.ToString(), "id=" + id);
        }

        public void setRank(int id, int rank)
        {
            Query.UpdateQuery(this.conn, "character_jobs", "job_rank", rank.ToString(), "id=" + id);
        }

        public void setLevel(int id, int level)
        {
            Query.UpdateQuery(this.conn, "character_jobs", "job_level", level.ToString(), "id=" + id);
        }

        public void setState(int id, int state)
        {
            Query.UpdateQuery(this.conn, "character_jobs", "job_state", state.ToString(), "id=" + id);
        }

        public void setAlias(int id, string alias)
        {
            Query.UpdateQuery(this.conn, "character_jobs", "job_alias", alias, "id=" + id);
        }
    }
}
